package com.drivemanage.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.drivemanage.dao.adminDao;
import com.drivemanage.entry.CarInfo;
import com.drivemanage.entry.DriveResult;
import com.drivemanage.entry.LogInfo;
import com.drivemanage.entry.OrderInfo;
import com.drivemanage.utils.OpenUtils;

@Service
@Transactional
public class adminManServiceImpl implements adminManService {

    @Resource
    private adminDao admindao;

    @Override
    public DriveResult getOrders(String orderType, String orderby) {
        int typenum = 0;
        int sortnum = 0;
        if ("全部订单".equals(orderType)) {
            typenum = 1;
        }
        else if ("进行中订单".equals(orderType)) {
            typenum = 2;
        }
        else if ("已完成订单".equals(orderType)) {
            typenum = 3;
        }
        if ("默认排序".equals(orderby)) {
            sortnum = 1;
        }
        else if ("按时间".equals(orderby)) {
            sortnum = 2;
        }
        else if ("按状态".equals(orderby)) {
            sortnum = 3;
        }
        Map paramMap = new HashMap();
        paramMap.put("typenum", typenum);
        paramMap.put("sortnum", sortnum);
        List<OrderInfo> orderInfo = admindao.bigSelectOrder(paramMap);
        return new DriveResult(DriveResult.SUCCESS_CODE, "查询成功", orderInfo);
    }

    @Override
    public DriveResult getManageInfo(String orderType, String orderby) {
        int typenum = 0;
        int sortnum = 0;
        Map param = new HashMap();
        param.put("sortnum", sortnum);
        if ("会员管理".equals(orderType)) {
            if ("按注册时间".equals(orderby)) {
                sortnum = 1;
                param.put("sortnum", sortnum);
            }
            List<LogInfo> logInfo = admindao.getLogInfo(param);
            return new DriveResult(DriveResult.SUCCESS_CODE, "查询成功", logInfo);
        }
        if ("车辆管理".equals(orderType)) {
            if ("按名称".equals(orderby)) {
                sortnum = 1;
                param.put("sortnum", sortnum);
            }
            if ("按售价".equals(orderby)) {
                sortnum = 2;
                param.put("sortnum", sortnum);
            }
            List<CarInfo> carInfo = admindao.getCarInfo(param);
            return new DriveResult(DriveResult.SUCCESS_CODE, "查询成功", carInfo);
        }
        if ("订单管理".equals(orderType)) {
            if ("按时间".equals(orderby)) {
                sortnum = 1;
                param.put("sortnum", sortnum);
            }
            if ("按状态".equals(orderby)) {
                sortnum = 2;
                param.put("sortnum", sortnum);
            }
            List<OrderInfo> orderInfo = admindao.getOrderInfo(param);
            return new DriveResult(DriveResult.SUCCESS_CODE, "查询成功", orderInfo);
        }
        if ("体验中心".equals(orderType)) {
            if ("按时间".equals(orderby)) {
                sortnum = 1;
                param.put("sortnum", sortnum);
            }
            List<Map> expInfo = admindao.getExpInfo(param);
            return new DriveResult(DriveResult.SUCCESS_CODE, "查询成功", expInfo);
        }
        return new DriveResult(DriveResult.ERROR_CODE, "未查询到相关信息", "");
    }

    @Override
    public DriveResult changeOrderStatus(String tag, String order_id) {
        String order_status = "";
        if ("接单".equals(tag)) {
            order_status = "1";
            Map param = OpenUtils.asMap("order_status", order_status, "order_id", order_id);
            admindao.changeOrderStatus(param);
            return new DriveResult(DriveResult.SUCCESS_CODE, "接单成功，请尽快分配车辆", "");
        }
        if ("拒绝".equals(tag)) {
            order_status = "4";
            Map param = OpenUtils.asMap("order_status", order_status, "order_id", order_id);
            admindao.changeOrderStatus(param);
            return new DriveResult(DriveResult.SUCCESS_CODE, "已拒绝订单，并通知用户", "");
        }
        return new DriveResult(DriveResult.ERROR_CODE, "未知错误发生，请重试！", "");

    }

    @Override
    public DriveResult deluser(String user_id) {
        admindao.deluser(user_id);
        return new DriveResult(DriveResult.SUCCESS_CODE, "删除成功", "");
    }

    @Override
    public DriveResult delcars(String car_id) {
        admindao.delcars(car_id);
        return new DriveResult(DriveResult.SUCCESS_CODE, "删除成功", "");
    }

    @Override
    public DriveResult delorder(String order_id) {
        admindao.delorder(order_id);
        return new DriveResult(DriveResult.SUCCESS_CODE, "删除成功", "");
    }

    @Override
    public DriveResult deltour(String art_id) {
        admindao.deltour(art_id);
        return new DriveResult(DriveResult.SUCCESS_CODE, "删除成功", "");
    }
}
